//
//  YZYTabBar.swift
//  Y微Z博Y
//
//  Created by mac on 16/10/22.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit

class YZYTabBar: UITabBar {
    // 01 定义一个闭包
    var closure: (() -> ())?

    // 如果init(frame) 就代表你要用手写代码方式创建该类
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func buttonClick() {
        // 03 执行闭包
        closure?()
    }
    
    // MARK: - 设置视图
    private func setupUI() {
        // 添加控件
        addSubview(composeButton)
    }
    
    // MARK: - 添加控件
    private lazy var composeButton: UIButton = {
        let button = UIButton(setImgName: "tabbar_compose_icon_add", backgroundImgName: "tabbar_compose_button", target: self, action: #selector(buttonClick))
    
//        // 点击事件
//        button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
//        // 设置图片
//        button.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControlState.normal)
//        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: UIControlState.selected)
//        // 设置背景图片
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControlState.normal)
//        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControlState.selected)
//        // 填充模式
//        button.sizeToFit()
        
        return button
    }()
    
    // 布局UITabBarButton
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = YZY_W * 0.2
        var index: CGFloat = 0
        // 遍历subViews
        for value in subviews {
            //判断 class
            if value.isKind(of: NSClassFromString("UITabBarButton")!) {
                value.frame.size.width = w
                value.frame.origin.x = index * w
                // 累加
                index += 1
                // 判断给➕号位置
                if index == 2 {
                    index += 1
                }
            }
        }
        // 设置➕号按钮的frame
        composeButton.center.x = YZY_W * 0.5
        composeButton.center.y = frame.size.height * 0.5
    }
}








